package com.rede.didiok.user.modulelist.file.service.impl;

import java.io.File;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.utils.DateUtils;
import com.rede.didiok.common.utils.FileUtils;
import com.rede.didiok.user.modulelist.file.annotation.UploadType;
import com.rede.didiok.user.modulelist.file.entity.FileCategoryEntity;
import com.rede.didiok.user.modulelist.file.enums.UploadTypeEnum;
import com.rede.didiok.user.modulelist.file.util.MinioUtil;
import com.rede.didiok.user.modulelist.file.util.MoGuFileUtil;


/**
 * 本地对象存储服务 Minio文件上传实现类
 *
 * @author 陌溪
 * @date 2020年10月19日11:13:21
 */
@Service
@Slf4j
@UploadType({UploadTypeEnum.MINIO_UPLOAD})
public class MinioServiceImpl extends AbstractUploadFile {

    @Autowired
    MinioUtil minioUtil;

    @Override
    public String uploadFile(SystemConfigDto systemConfig, File tempFile, FileCategoryEntity fileSort, String newFileName) {
        String sortUrl = org.apache.commons.lang3.StringUtils.defaultIfEmpty(fileSort.getUrl(), "base/common/");
        //获取新文件名 【根据扩展名保存】
        String uploadDir = sortUrl + "/" + FileUtils.getPicExpandedName(newFileName) +"/" + DateUtils.getYears() + "/"
                + DateUtils.getMonth() + "/" + DateUtils.getDay() + "/" + newFileName;
        return minioUtil.uploadFile(systemConfig, tempFile, uploadDir);
    }

    @Override
    public String deleteFile(String fileName, SystemConfigDto systemConfig) {
        // 批量删除本地图片
        MoGuFileUtil.deleteFile(fileName);
        return minioUtil.deleteFile(systemConfig, fileName);
    }
}
